package com.goods.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author jiaoyanlin
 * @Date 2021/11/8 21:34
 * @Description ProductServiceImpl
 */
//TODO  怎么判断   根据分类id查询
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageVO<ProductVO> findProductList(Map<String, Object> queryMap) {
        int pageNum = Convert.toInt(queryMap.get("pageNum"), 5);
        String pageS = (String) queryMap.get("pageSize");
        int pageSize = Integer.parseInt(pageS);

        String name = (String) queryMap.get("name");
        String[] categorys = null;
        Object o = queryMap.get("categorys");

        String status = (String) queryMap.get("status");
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != o) {
            categorys = o.toString().split(",");
            if (categorys.length > 0) {
                if (categorys.length == 2) {
                    criteria.andEqualTo("oneCategoryId", Integer.parseInt(categorys[0]))
                            .andEqualTo("twoCategoryId", Integer.parseInt(categorys[1]));
                }
                if (categorys.length == 3) {
                    criteria.andEqualTo("oneCategoryId", Integer.parseInt(categorys[0]))
                            .andEqualTo("twoCategoryId", Integer.parseInt(categorys[1]))
                            .andEqualTo("threeCategoryId", Integer.parseInt(categorys[2]));
                }

            }
        }
        if(StrUtil.isNotEmpty(name)){
            //TODO 这么平到底有没有问题
            criteria.andLike("name", "%" + name + "%");
        }
        criteria.andEqualTo("status", Integer.parseInt(status));

     /*   if (null!=categorys) {
            criteria.orEqualTo("one_category_id", Integer.parseInt(categorys[0]))
                    .orEqualTo("two_category_id", Integer.parseInt(categorys[1]))
                    .orEqualTo("three_category_id", Integer.parseInt(categorys[2]));
        }*/

        List<Product> products = productMapper.selectByExample(example);
        List<ProductVO> productVoList = getProductVoList(products);
        return getProductVOPageVO(pageNum, pageSize, productVoList);
        //Integer.parseInt(categorys[0])
        //先判断条件  名字非空
   /*     if (!StringUtils.isEmpty(name)){
            //状态非空
            if (!StringUtils.isEmpty(status)){
                if (categorys.length>0){
                    criteria.andLike("name","%"+name+"%");
                    criteria.andEqualTo("status",Integer.parseInt(status));
                    criteria.andEqualTo("one_category_id",Integer.parseInt(categorys[0]))
                    .orEqualTo("two_category_id",Integer.parseInt(categorys[1]))
                            .orEqualTo("three_category_id",Integer.parseInt(categorys[2]));
                    List<Product> products = productMapper.selectByExample(example);
                    List<ProductVO> productVoList = getProductVoList(products);
                    return getProductVOPageVO(pageNum, pageSize,productVoList);
                }else {
                    criteria.andLike("name","%"+name+"%");
                    criteria.andEqualTo("status",Integer.parseInt(status));

                    List<Product> products = productMapper.selectByExample(example);
                    List<ProductVO> productVoList = getProductVoList(products);
                    return getProductVOPageVO(pageNum, pageSize,productVoList);
                }

            }else {
                criteria.andLike("name","%"+name+"%");
                List<Product> products = productMapper.selectByExample(example);
                List<ProductVO> productVoList = getProductVoList(products);
                return getProductVOPageVO(pageNum, pageSize,productVoList);
            }

        }else {
            //名字为空  状态不为空
             if (!StringUtils.isEmpty(status)){
                 criteria.andEqualTo("status",Integer.parseInt(status));
                 List<Product> products = productMapper.selectByExample(example);
                 List<ProductVO> productVoList = getProductVoList(products);
                 return getProductVOPageVO(pageNum, pageSize,productVoList);
             }
             else {
                 //都为空 那就是查全部
                 List<Product> products = productMapper.selectAll();
                 List<ProductVO> productVOList = getProductVoList(products);

                 return getProductVOPageVO(pageNum, pageSize, productVOList);
             }

        }


        //判断分类id数组是不是空，不是空 再判断有没有物质名称 根据名称模糊查

       */


    }

    //分页返回PageVO<ProductVO>
    private PageVO<ProductVO> getProductVOPageVO(int pageNum, int pageSize, List<ProductVO> productVOList) {
        PageHelper.startPage(pageNum, pageSize);
        if(CollUtil.isEmpty(productVOList)){
            productVOList = new ArrayList<>();
        }
        List<ProductVO> page = ListPageUtils.page(productVOList, pageSize, pageNum);
        if(CollUtil.isEmpty(page)){
            page = new ArrayList<>();
        }
        PageInfo<ProductVO> info = new PageInfo<>(page);
        return new PageVO<>(info.getTotal(), productVOList);
    }

    //List<Product>------>>>>转成List<ProductVO>
    public List<ProductVO> getProductVoList(List<Product> productList) {
        List<ProductVO> productVOList = new ArrayList<>();

        for (Product product : productList) {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            productVOList.add(productVO);
        }
        return productVOList;
    }

    @Override
    public ProductVO edit(String id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product, productVO);
        return productVO;
    }

    @Override
    public void update(ProductVO productVo, Long id) {
        Product product = new Product();
        productVo.setId(id);
        BeanUtils.copyProperties(productVo, product);
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void back(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void add(Map<String,Object > map) {
        /*Product product = new Product();
        BeanUtils.copyProperties(productVO, product);*/
//        productMapper.insert(product);
    }

    @Override
    public void remove(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(2);
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void publish(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        productMapper.updateByPrimaryKey(product);
    }

    //库村 图形化 查询
@Autowired
private ProductStockMapper productStockMapper;
    @Override
    public PageVO<ProductStockVO> findProductStocks(Map<String, Object> map) {

        int pageNum = Convert.toInt(map.get("pageNum"));
        int pageSize = Convert.toInt(map.get("pageSize"));
        PageHelper.startPage(pageNum,pageSize);
        Object o = map.get("categorys");
        String[] categorys =null;
                Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != o) {
            categorys= o.toString().split(",");
            if (categorys.length > 0) {
                if (categorys.length == 2) {
                    criteria.andEqualTo("oneCategoryId", Integer.parseInt(categorys[0]))
                            .andEqualTo("twoCategoryId", Integer.parseInt(categorys[1]));
                }
                if (categorys.length == 3) {
                    criteria.andEqualTo("oneCategoryId", Integer.parseInt(categorys[0]))
                            .andEqualTo("twoCategoryId", Integer.parseInt(categorys[1]))
                            .andEqualTo("threeCategoryId", Integer.parseInt(categorys[2]));
                }
            }
        }
        List<ProductStockVO> productStockVOList =new ArrayList<>();
        List<Product> products = productMapper.selectByExample(example);



        if (!CollectionUtils.isEmpty(products)){
            for (Product product : products) {

                ProductStockVO productStockVO = new ProductStockVO();
                //根据product中的pnum 查对应库存表 数量
                if (!StringUtils.isEmpty(product.getPNum())){
                    Example example1 =new Example(ProductStock.class);
                    Example.Criteria c = example1.createCriteria();
                    c.andEqualTo("pNum",product.getPNum());
                    ProductStock productStock = productStockMapper.selectOneByExample(example1);
                     if (productStock == null) {
                       //没有库存 那库存值就设置为0
                        productStockVO.setStock(Long.parseLong("0"));
                    }else {
                        productStockVO.setStock(productStock.getStock());
                    }


                }
                productStockVO.setName(product.getName());
                productStockVO.setImageUrl(product.getImageUrl());
                productStockVO.setModel(product.getModel());
                productStockVO.setRemark(product.getRemark());
                productStockVO.setUnit(product.getUnit());

                productStockVOList.add(productStockVO);
            }
        }
        List<ProductStockVO> page = ListPageUtils.page(productStockVOList, pageSize, pageNum);

        if (CollectionUtils.isEmpty(page)){
          page=new ArrayList<>();
        }
        PageInfo<ProductStockVO> info = new PageInfo<>(page);
       return new PageVO<ProductStockVO>(info.getTotal(),productStockVOList);


    }

    @Override
    public PageVO<ProductStockVO> findAllStocks(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductStock> productStocks = productStockMapper.selectAll();
        List<ProductStockVO> stockVOList = productStocks.stream().map(productStock -> {
            ProductStockVO productStockVO = new ProductStockVO();
            BeanUtils.copyProperties(productStock, productStockVO);
            return productStockVO;
        }).collect(Collectors.toList());

        List<ProductStockVO> page = ListPageUtils.page(stockVOList, pageSize, pageNum);

        if (CollectionUtils.isEmpty(page)){
            page=new ArrayList<>();
        }
        PageInfo<ProductStockVO> info = new PageInfo<>(page);
        return new PageVO<>(info.getTotal(),stockVOList);
    }
}
